package com.parsifal.bluetoothdemo.socket;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.parsifal.bluetoothdemo.listener.OnSocketReceiveListener;
import com.parsifal.bluetoothdemo.listener.OnSocketSendListener;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by yangming on 17-3-31.
 */
public class Client {

    private static final String TAG = "Client";

    private BluetoothSocket mClientSocket = null;

    private boolean mClientRun = false;

    private OnSocketSendListener mOnSendListener = null;

    private OnSocketReceiveListener mOnReceiveListener = null;

    private ExecutorService mThreadPool = null;

    public void connect(BluetoothDevice device) {
        try {
            if (!mClientRun) {
                mThreadPool = Executors.newFixedThreadPool(2);
                ClientThread clientThread = new ClientThread(device) {
                    @Override
                    public void run() {
                        try {
                            mClientSocket = getDevice().createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                            mClientSocket.connect();
                            Log.i(TAG, "蓝牙客户端ON");
                            mClientRun = true;

                            while (mClientRun) {
                                InputStream inputStream = mClientSocket.getInputStream();
                                byte[] buffer = new byte[1024];
                                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                                int length = bufferedInputStream.read(buffer);
                                String serverMsg = new String(buffer, 0, length);
                                if (null != mOnReceiveListener) {
                                    mOnReceiveListener.onReceive(serverMsg);
                                }
                            }
                        } catch (Exception e) {
                            mThreadPool.shutdownNow();
                            e.printStackTrace();
                        }
                    }
                };
                mThreadPool.execute(clientThread);
            }
        } catch (Exception e) {
            try {
                mClientSocket.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    public void send(final String clientMsg) {
        try {
            mThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        OutputStream outputStream = mClientSocket.getOutputStream();
                        outputStream.write(clientMsg.getBytes());
                        outputStream.flush();
                        if (null != mOnSendListener) {
                            mOnSendListener.onSend(clientMsg);
                        }
                    } catch (Exception e) {
                        try {
                            mClientSocket.close();
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnSendListener(OnSocketSendListener listener) {
        mOnSendListener = listener;
    }

    public void setOnReceiveListener(OnSocketReceiveListener listener) {
        mOnReceiveListener = listener;
    }

    public void stopClient() {
        mClientRun = false;
        if (null != mClientSocket) {
            try {
                mClientSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != mThreadPool) {
            mThreadPool.shutdownNow();
        }
    }
}
